package cn.jiahua.hospital.service.impl;

import ch.qos.logback.core.net.SyslogOutputStream;
import cn.jiahua.hospital.constants.Constants;
import cn.jiahua.hospital.exceptions.RedisException;
import cn.jiahua.hospital.exceptions.TokenProtectedException;
import cn.jiahua.hospital.pojo.Doctor;
import cn.jiahua.hospital.pojo.Hospital;
import cn.jiahua.hospital.pojo.User;
import cn.jiahua.hospital.service.TokenService;
import cn.jiahua.hospital.utils.RedisUtil;
import cn.jiahua.hospital.vo.TokenVo;
import cn.jiahua.hospital.vo.WxTokenVo;
import cn.jiahua.hospital.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @ClassName TokenServiceImpl
 * @Description TODO
 * @Author yaozhaobao
 * @Date 2021/4/6 11:30
 * @Version 1.0
 **/
@Service
public class TokenServiceImpl implements TokenService {
    @Autowired
    RedisUtil redisUtil;


    @Override
    public TokenVo genenrateToken(User user) {
        // String tokenKey="token:"+System.currentTimeMillis()+"_"+MD5Utils.MD5(users.getAccount()).substring(26)+"_"+users.getAccount();
        Long gentime = System.currentTimeMillis();
        String account = user.getUserPhone() != null ? user.getUserPhone() : user.getUserWeChatOpenId();
        String tokenKey = "token:pc-" + gentime + "-" + UUID.randomUUID().toString().replace("-", "").substring(26) + "-" + account;
        TokenVo tokenVo = new TokenVo();
        tokenVo.setCreateTime(gentime);
        tokenVo.setToken(tokenKey);
        tokenVo.setOvertime(gentime + Constants.Token.VALID_TIME);
        return tokenVo;
    }


    @Override
    public TokenVo refreshToken(String oldtoken) throws RedisException, TokenProtectedException {
        //1.有效性校验,校验redis中有没有token
        if (!redisUtil.hasKey(oldtoken)) {
            throw new RedisException("redis不存在此token,过期了,请重新登录");
        }
        //2.oldtoken是否没有达到校验的时间，替换时间是oldtoken过期时间的前5分钟
        String oldTokenTime = oldtoken.split("-")[1];
        if (System.currentTimeMillis() - Long.parseLong(oldTokenTime) < Constants.Token.VALID_TIME - Constants.Token.NO_PROTEST_TIME) {
            throw new TokenProtectedException("token还在保护期间，不允许置换token");
        }

        //3.生成新的token
        User user = (User) redisUtil.get(oldtoken);
        TokenVo tokenVo = genenrateToken(user);
        //4.获取微信的accesstoken
        Object o = redisUtil.get("wx:" + tokenVo.getToken());
        if (o != null) {
            WxTokenVo wxTokenVo = (WxTokenVo) o;
            wxTokenVo.setTokenVo(tokenVo);
            redisUtil.set("wx:" + tokenVo.getToken(), wxTokenVo, Constants.Token.WX_VALID_TIME);
        }
        //4.新旧token替换
        saveToken(tokenVo.getToken(), user);

        //5.旧的token续钟命2分
        redisUtil.set(oldtoken, user, Constants.Token.DELAY_TIME);
        return tokenVo;
    }

    @Override
    public void saveToken(String token, User user) {
        redisUtil.set(token, user, Constants.Duration.HALF_AN_HOUR);
    }



    @Override
    public TokenVo genenrateToken(Hospital hospital) {
        // String tokenKey="token:"+System.currentTimeMillis()+"_"+MD5Utils.MD5(users.getAccount()).substring(26)+"_"+users.getAccount();
        Long gentime = System.currentTimeMillis();
        String account = hospital.getHospitalphone();
        String tokenKey = "token:pc-" + gentime + "-" + UUID.randomUUID().toString().replace("-", "").substring(26) + "-" + account;
        TokenVo tokenVo = new TokenVo();
        tokenVo.setCreateTime(gentime);
        tokenVo.setToken(tokenKey);
        tokenVo.setOvertime(gentime + Constants.Token.VALID_TIME);
        return tokenVo;
    }

    @Override
    public void saveToken(String token, Hospital hospital) {
        redisUtil.set(token, hospital, Constants.Duration.HALF_AN_HOUR);
    }


    @Override
    public TokenVo genenrateToken(Doctor doctor) {
        // String tokenKey="token:"+System.currentTimeMillis()+"_"+MD5Utils.MD5(users.getAccount()).substring(26)+"_"+users.getAccount();
        Long gentime = System.currentTimeMillis();
        String account = doctor.getPhone();
        String tokenKey = "token:pc-" + gentime + "-" + UUID.randomUUID().toString().replace("-", "").substring(26) + "-" + account;
        TokenVo tokenVo = new TokenVo();
        tokenVo.setCreateTime(gentime);
        tokenVo.setToken(tokenKey);
        tokenVo.setOvertime(gentime + Constants.Token.VALID_TIME);
        return tokenVo;
    }

    @Override
    public void saveToken(String token, Doctor doctor) {
        redisUtil.set(token, doctor, Constants.Duration.HALF_AN_HOUR);
    }
}
